#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osgViewer/Viewer>
#include <osg/StateSet>
#include <osg/BlendFunc>
#include "../src/core/CColor.h"

osg::Node* createModel(const osg::Vec3& pos, const osg::Vec4& color, const std::string& name, bool isTransparent) {
    osg::Geode* geode = new osg::Geode();
    osg::ShapeDrawable* shape = new osg::ShapeDrawable(new osg::Box(pos, 1.0f));
    shape->setColor(color);
    shape->setName(name);
    geode->addDrawable(shape);
    
    if (isTransparent) {
        osg::StateSet* stateset = geode->getOrCreateStateSet();
        stateset->setMode(GL_BLEND, osg::StateAttribute::ON);
        //stateset->setAttributeAndModes(new osg::BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA));
        stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
        //stateset->setRenderBinDetails(10, "DepthSortedBin");// automatic depth sorting: avoiding the uncertainty of implicit behavior
    }
    
    return geode;
}

int runTest() {
    osg::ref_ptr<osg::Group> root = new osg::Group();
    
    CColor pink = "lightpink";
    osg::Node* redOpaque = createModel(osg::Vec3(0, 0, 0.1),     osg::Vec4(1,0,0,1),   "A", false);// Red Opaque
    osg::Node* greenOpaque = createModel(osg::Vec3(0.5,0.5,0.2), osg::Vec4(0,1,0,1),   "B", false);// Green Opaque
    osg::Node* yellowTrans = createModel(osg::Vec3(0.3,0.3,0.3), osg::Vec4(pink.redF(),pink.greenF(),pink.blueF(),0.5), "a", true);// Pink Transparent
    osg::Node* blueTrans = createModel(osg::Vec3(0.7,0.7,0.4),   osg::Vec4(0,0,1,0.5), "b", true);// Blue Transparent
    
    // Add child nodes in any order
    root->addChild(yellowTrans);
    root->addChild(greenOpaque);
    root->addChild(redOpaque);
    root->addChild(blueTrans);
    
    osgViewer::Viewer viewer;
    viewer.setUpViewInWindow(200, 200, 800, 600);
    viewer.setSceneData(root);
    viewer.getCamera()->setClearColor(osg::Vec4(0, 0, 0, 1));
    
    return viewer.run();
}